package com.xiyu.service.service.statistics;

import com.xiyu.service.service.setting.BusinessFieldService;
import com.xiyu.service.service.system.user.UserService;
import com.xiyu.service.vo.statistics.statisticsCustomer.StatisticsCustomerDealConvertQueryOutput;
import com.xiyu.service.vo.statistics.statisticsCustomer.StatisticsCustomerDealConvertQueryInput;
import com.xiyu.service.convert.statistics.StatisticsCustomerConvert;
import com.xiyu.service.vo.statistics.statisticsCustomer.StatisticsCustomerKeyTrendQueryOutput;
import com.xiyu.service.vo.statistics.statisticsCustomer.StatisticsCustomerKeyTrendQueryInput;
import com.xiyu.service.repository.customer.follow.CustomerFollowRepository;
import com.xiyu.service.repository.customer.info.CustomerInfoRepository;
import org.babyfish.jimmer.sql.ast.tuple.Tuple3;
import org.babyfish.jimmer.sql.ast.tuple.Tuple4;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;


import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 客户统计 Service 实现类
 */
@Service
@Validated
public class StatisticsCustomerServiceImpl implements StatisticsCustomerService {

    @Resource
    CustomerInfoRepository customerInfoRepository;

    @Resource
    CustomerFollowRepository customerFollowRepository;

    @Resource
    UserService userService;

    @Resource
    BusinessFieldService businessFieldService;


    @Override
    public List<StatisticsCustomerKeyTrendQueryOutput> keyTrendQuery(StatisticsCustomerKeyTrendQueryInput inputVO) {
        List<Long> ownerUserIds = userService.getOwnerUserIds(getLoginUserId());
        List<Tuple3<Long, LocalDateTime, Long>> createCount = customerInfoRepository.keyTrendQuery(ownerUserIds, inputVO.getOwnerIds(), inputVO.getStatisticsTime());
        List<Tuple3<Long, LocalDateTime, Long>> followCount = customerFollowRepository.keyTrendQuery(ownerUserIds, inputVO.getOwnerIds(), inputVO.getStatisticsTime());
        List<Tuple4<Long, LocalDateTime, Long, Long>> mergeCount = mergeLists(createCount, followCount);
        return StatisticsCustomerConvert.INSTANCE.keyTrendQueryOutputConvert(mergeCount);
    }

    public static List<Tuple4<Long, LocalDateTime, Long, Long>> mergeLists(
            List<Tuple3<Long, LocalDateTime, Long>> createCount,
            List<Tuple3<Long, LocalDateTime, Long>> followCount) {

        // 将两个列表合并到一个 Map 中，以第二个参数 (Long) 和第三个参数 (LocalDateTime) 为键
        Map<String, Tuple4<Long, LocalDateTime, Long, Long>> resultMap = new HashMap<>();

        // 处理 createCount 列表
        for (Tuple3<Long, LocalDateTime, Long> tuple : createCount) {
            String key = tuple.component1() + "|" + tuple.component2(); // 使用 Long 和 LocalDateTime 拼接作为唯一键
            resultMap.put(key, new Tuple4<>(tuple.component1(), tuple.component2(), tuple.component3(), 0L));
        }

        // 处理 followCount 列表
        for (Tuple3<Long, LocalDateTime, Long> tuple : followCount) {
            String key = tuple.component1() + "|" + tuple.component2(); // 使用 Long 和 LocalDateTime 拼接作为唯一键
            if (resultMap.containsKey(key)) {
                // 如果键已存在，则更新 Tuple4 的第三个值
                Tuple4<Long, LocalDateTime, Long, Long> existing = resultMap.get(key);
                resultMap.put(key, new Tuple4<>(existing.component1(), existing.component2(), existing.component3(), tuple.component3()));
            } else {
                // 如果键不存在，则直接插入新的 Tuple4
                resultMap.put(key, new Tuple4<>(tuple.component1(), tuple.component2(), 0L, tuple.component3()));
            }
        }

        // 返回合并后的结果列表
        return new ArrayList<>(resultMap.values());
    }

    @Override
    public List<StatisticsCustomerDealConvertQueryOutput> dealConvertQuery(StatisticsCustomerDealConvertQueryInput inputVO) {
        List<Long> ownerUserIds = userService.getOwnerUserIds(getLoginUserId());
        Long stateDealOptionId = businessFieldService.getStateDealOptionId();
        List<Tuple3<Long, Long, Long>> dealCount = customerInfoRepository.dealConvertQuery(ownerUserIds, inputVO.getOwnerIds(), stateDealOptionId);
        return StatisticsCustomerConvert.INSTANCE.dealConvertQueryOutputConvert(dealCount);
    }

}
